using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Nate_sRoguelikeGameXNA
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        # region Static
        # region Fields and Properties
        internal static IngameScreen IngameScreen { get; private set; }
        public static Random Random = new Random();
        public static SpriteFont Consolas, ConsolasBold;

        # endregion
        # region Methods

        public static Vector2 GetScreenCoords(int x, int y)
        {
            return new Vector2(x * (graphics.GraphicsDevice.Viewport.Width / 80f),
                               y * (graphics.GraphicsDevice.Viewport.Height / 35f));
        }

        public static Vector2 GetScreenCoordsWithOffset(int x, int y)
        {
            return new Vector2((x + IngameScreen.DrawingOffset.X) * (graphics.GraphicsDevice.Viewport.Width / 80f),
                               (y + IngameScreen.DrawingOffset.Y) * (graphics.GraphicsDevice.Viewport.Height / 35f));
        }

        public static Vector2 GetScreenCoordsWithOffset(Point point)
        {
            return GetScreenCoordsWithOffset(point.X, point.Y);
        }
        public static int Roll(int dice, int sides, bool isZeroBased = false)
        {
            return Roll(dice, sides, 0, isZeroBased);
        }

        public static int Roll(int dice, int sides, int[] numbersToExclude, bool isZeroBased = false)
        {
            var roll = Roll(dice, sides, 0, isZeroBased);
            return numbersToExclude.Contains(roll) ? Roll(dice, sides, numbersToExclude, isZeroBased) : roll;
        }

        public static int Roll(int dice, int sides, int minRoll, bool isZeroBased = false)
        {
            int result = 0;
            for (int i = 0; i < dice; i++)
                result += Random.Next(sides + (isZeroBased ? 0 : 1));
            return Math.Max(minRoll, result);
        }

        public static int Roll(int dice, int sides, int minRoll, int maxRoll, int modifier = 0, bool isZeroBased = false)
        {
            int result = 0;
            for (int i = 0; i < dice; i++)
                result += Random.Next(sides + (isZeroBased ? 0 : 1));
            result += modifier;
            return Math.Min(Math.Max(minRoll, result), maxRoll);
        }
        # endregion
        # endregion
        # region Instance
        # region Fields

        static GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        bool drawScreen = true;
        private Screen _currentScreen;
        public static bool Debug = false;
        # endregion

        # region Constructor

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 880;
            graphics.PreferredBackBufferHeight = 600;
            IsMouseVisible = true;
            Content.RootDirectory = "Content";
            Rectangle r = new Rectangle(14, 14, 6, 7);
            var contains = r.ReallyContains(new Point(18, 13));
        }
        # endregion

        # region Methods
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            IngameScreen = new IngameScreen(spriteBatch);
            _currentScreen = IngameScreen;
            IngameScreen.Initialize();
            Consolas = Content.Load<SpriteFont>("Consolas");
            ConsolasBold = Content.Load<SpriteFont>("ConsolasBold");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            _currentScreen.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            _currentScreen.Draw(gameTime);

            base.Draw(gameTime);
        }

        # endregion
        # endregion
    }
}
